# -*- coding: utf-8 -*-

from django.http.response import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User
from django.core import serializers
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib.auth import authenticate, login

def log_in(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username = username, password = password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            request.session['alerta'] = 'Usuário inativo.'
        return HttpResponseRedirect('/')
    else:
        request.session['alerta'] = 'Usuário e/ou senha inválido(s).'
        return HttpResponseRedirect('/')
        

def usuarios(request):
    if request.is_ajax():
        usuarios = User.objects.all()
        
        format = 'json'
        mimetype = 'application/json'

        retorno = serializers.serialize(format, usuarios)
        return HttpResponse(retorno, mimetype)
    else:
        return HttpResponse(None)

def sobre(request):
    return render_to_response('sobre.html', locals(),
        context_instance=RequestContext(request))

def ajuda(request):
    return render_to_response('ajuda.html', locals(),
        context_instance=RequestContext(request))

def contatos(request):
    css = "contatos"
    return render_to_response('contatos.html', locals(),
        context_instance=RequestContext(request))
